package top.xtcoder.jdcbase.base.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.lang.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import top.xtcoder.jdcbase.base.core.Resp;
import top.xtcoder.jdcbase.base.core.UserType;
import top.xtcoder.jdcbase.base.entity.base.NationInfo;

@Api(tags = "民族模块")
@RestController
@RequestMapping("/jdcbase/prbase/nation")
public class PrBaseNationController {
	@Autowired
	private Dao dao;
	
	@ApiOperation(value="添加/编辑")
	@PostMapping("/addOrUpdate")
	@SaCheckPermission({UserType.Admin.permName})
	public Resp<NationInfo> addOrUpdate(@Valid @RequestBody NationInfo at){
		at = (@Valid NationInfo) at.insertOrUpdate(dao);
		return Resp.OBJ_O(at);
	}

	@ApiOperation(value = "软删除")
	@DeleteMapping("/delete")
	public Resp delete(@RequestParam("id") String id
			, HttpServletRequest request) {
		if (Strings.isBlank(id)) {
			return new Resp().error("id不能为空");
		}
		NationInfo tea = dao.fetch(NationInfo.class, id);
		if (tea == null) {
			return new Resp().error("该数据已经被删除了");
		}
		int num = tea.deletedSoft(dao);
		return Resp.OBJ_O(num);
	}
	
	@ApiOperation("查询")
	@GetMapping("/list")
	@SaCheckPermission({UserType.Admin.permName})
	public Resp<List<NationInfo>> list(){
		Cnd cnd = Cnd.where("isdelete", "=", 0);
		List<NationInfo> list = dao.query(NationInfo.class, cnd.asc("sort"));
		return Resp.LIST_Q(list);
	}
}
